package com.example.springboot_sp.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot_sp.service.impl.CalendarServiceImpl;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import com.example.springboot_sp.common.Result;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.example.springboot_sp.service.ICalendarService;
import com.example.springboot_sp.entity.Calendar;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 郭守军
 * @since 2022-09-16
 */
@RestController
@RequestMapping("/calendar")
        public class CalendarController {
    
        @Resource
        private CalendarServiceImpl calendarService;

        // 新增或者更新
        @PostMapping
        public Result save(@RequestBody Calendar calendar) {
                return Result.success(calendarService.saveOrUpdate(calendar));
                }

        @DeleteMapping("/{id}")
        public Result delete(@PathVariable Integer id) {
                return Result.success(calendarService.removeById(id));
                }

        @PostMapping("/del/batch")
        public Result deleteBatch(@RequestBody List<Integer> ids) {
                return Result.success(calendarService.removeByIds(ids));
                }

        @GetMapping
        public Result findAll() {
                return Result.success(calendarService.list());
                }

        @GetMapping("/date")
        public Result date(@RequestParam String date) {
                return Result.success(calendarService.findAllByDate(date));
        }

        @GetMapping("/{id}")
        public Result findOne(@PathVariable Integer id) {
                return Result.success(calendarService.getById(id));
                }

        @GetMapping("/page")
        public Result findPage(@RequestParam Integer pageNum,
        @RequestParam Integer pageSize) {
                QueryWrapper<Calendar> queryWrapper = new QueryWrapper<>();
                queryWrapper.orderByDesc("id");
                return Result.success(calendarService.page(new Page<>(pageNum, pageSize), queryWrapper));
                }
}

